﻿<template>
  <div class="deviceInfo-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }"> 
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="设备名称">
              <el-input v-model="queryParams.deviceName" clearable="" placeholder="请输入设备名称"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="设备编号">
              <el-input v-model="queryParams.deviceNo" clearable="" placeholder="请输入设备编号"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="描述">
              <el-input v-model="queryParams.description" clearable="" placeholder="请输入描述"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="地址">
              <el-input v-model="queryParams.address" clearable="" placeholder="请输入地址"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="位置">
              <el-input v-model="queryParams.position" clearable="" placeholder="请输入位置"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="位置类型">
              <el-select clearable="" v-model="queryParams.devicePositionType" placeholder="请选择位置类型">
                <el-option v-for="(item,index) in dl('DevicePositionType')" :key="index" :value="item.value" :label="`${item.name} (${item.code}) [${item.value}] `" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="设备网关ID">
              <el-select clearable="" filterable="" v-model="queryParams.deviceGateWayId" placeholder="请选择设备网关ID">
                <el-option v-for="(item,index) in deviceGateWayDeviceGateWayIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="设备模板ID">
              <el-select clearable="" filterable="" v-model="queryParams.deviceTemplateInfoId" placeholder="请选择设备模板ID">
                <el-option v-for="(item,index) in deviceTemplateInfoDeviceTemplateInfoIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item >
              <el-button-group style="display: flex; align-items: center;">
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'deviceInfo:page'"> 查询 </el-button>
                      <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                        <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI" style="margin-left:5px;"> 高级查询 </el-button>
                        <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI" style="margin-left:5px;"> 隐藏 </el-button>
                <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="openAddDeviceInfo" v-auth="'deviceInfo:add'"> 新增 </el-button>
                <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="refleshDeviceCurrentInfo" v-auth="'deviceInfo:add'"> 刷新配置 </el-button>
                
              </el-button-group>
            </el-form-item>
            
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 5px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
                row-key="id"
                @sort-change="sortChange"
				border="">
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="active" label="激活状态"  show-overflow-tooltip="" >
            <template #default="scope">
              <el-tag :type="dv('Active', scope.row.active)?.tagType"> {{dv('Active', scope.row.active)?.name}}</el-tag>
            </template>
          </el-table-column>
        <el-table-column prop="deviceName" label="设备名称"  show-overflow-tooltip="" />
        <el-table-column prop="deviceNo" label="设备编号"  show-overflow-tooltip="" />
        <el-table-column prop="description" label="描述"  show-overflow-tooltip="" />
        <el-table-column prop="address" label="地址"  show-overflow-tooltip="" />
        <el-table-column prop="position" label="位置"  show-overflow-tooltip="" />
          <el-table-column prop="devicePositionType" label="位置类型"  show-overflow-tooltip="" >
            <template #default="scope">
              <el-tag :type="dv('DevicePositionType', scope.row.devicePositionType)?.tagType"> {{dv('DevicePositionType', scope.row.devicePositionType)?.name}}</el-tag>
            </template>
          </el-table-column>
        <el-table-column prop="deviceGateWayId" label="设备网关ID"  show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.deviceGateWayIdGateWayName}}</span>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="deviceTemplateInfoId" label="设备模板ID"  show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.deviceTemplateInfoIdTemplateName}}</span>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="orgId" label="直属机构Id"  show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.orgIdName}}</span>
          </template>
          
        </el-table-column>
        <el-table-column label="操作" width="400" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('deviceInfo:update') || auth('deviceInfo:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditDeviceInfo(scope.row)" v-auth="'deviceInfo:update'"> 编辑 </el-button>
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openAddDeviceInfoValue(scope.row)" v-auth="'deviceInfo:delete'"> 详细 </el-button>
            <el-button icon="ele-Document" size="small" text="" type="primary" @click="openDeviceLogDialog(scope.row)" > 日志 </el-button>
            <el-button icon="ele-Document" size="small" text="" type="primary" @click="openDeviceCurrentDialog(scope.row)" >状态  </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delDeviceInfo(scope.row)" v-auth="'deviceInfo:delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
     
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200, 500]"
				size="small"
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <printDialog
        ref="printDialogRef"
        :title="printDeviceInfoTitle"
        @reloadTable="handleQuery" />
   
      <editDialog
        ref="editDialogRef"
        :title="editDeviceInfoTitle"
        @reloadTable="handleQuery"
      />
      <deviceLogForDevice
        ref="deviceValueDialogRef"
        title="设备运行日志"
        @reloadTable="handleQuery"
      />
      <deviceCurrent
        ref="deviceCurrentDialogRef"
        title="设备状态"
        @reloadTable="handleQuery"
      />
      <deviceValueDialog
        ref="deviceValueDialogRef1"
        title="设备运行状态"
        @reloadTable="handleQuery"></deviceValueDialog>
    </el-card>
    
  </div>
</template>

<script lang="ts" setup="" name="deviceInfo">
  import { ref } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';

  import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  import { getDictLabelByVal as dv } from '/@/utils/dict-utils';
  import { formatDate } from '/@/utils/formatTime';
  import deviceLogForDevice from '/@/views/Device/deviceLog/component/deviceLogForDevice.vue';
  import deviceCurrent from "/@/views/Device/DeviceInfo/deviceInfo/deviceCurrent.vue";

  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
  import editDialog from '/@/views/Device/DeviceInfo/deviceInfo/component/editDialog.vue'
  import deviceValueDialog from '/@/views/Device/DeviceInfo/deviceInfo/deviceValue.vue'
  import { pageDeviceInfo, deleteDeviceInfo } from '/@/api/Device/DeviceInfo/deviceInfo';
  import { getDeviceGateWayDeviceGateWayIdDropdown } from '/@/api/Device/DeviceInfo/deviceInfo';
  import { getDeviceTemplateInfoDeviceTemplateInfoIdDropdown,refreshAllDevice } from '/@/api/Device/DeviceInfo/deviceInfo';
	import { getAPI } from '/@/utils/axios-utils';
	import { SysEnumApi } from '/@/api-services/api';
  import commonFunction from '/@/utils/commonFunction';

  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const deviceValueDialogRef = ref();
  const deviceCurrentDialogRef = ref();
  const deviceValueDialogRef1=ref();
  const deviceLogDialogVisible = ref(false);

  const loading = ref(false);
  const tableData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    field: 'createTime', // 默认的排序字段
    order: 'descending', // 排序方向
    descstr: 'descending', // 降序排序的关键字符
    total: 0 as any,
  });

  const printDeviceInfoTitle = ref("");
  const editDeviceInfoTitle = ref("");

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }

  // 查询操作
  const handleQuery = async () => {
    try {
      loading.value = true;
    var res = await pageDeviceInfo(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
    } catch (error) {
      loading.value = false;

    }
    
  };

  // 列排序
  const sortChange = async (column: any) => {
	tableParams.value.field = column.prop;
	tableParams.value.order = column.order;
	await handleQuery();
  };

  // 打开新增页面
  const openAddDeviceInfo = () => {
    editDeviceInfoTitle.value = '添加设备信息';
    editDialogRef.value.openDialog({});
  };

  const refleshDeviceCurrentInfo=async ()=>{
    await refreshAllDevice();
    ElMessage.success("刷新配置成功");
  }
  // 打开新增页面
  const openAddDeviceInfoValue = (row:any) => {
    //跳转页面#/Device/DeviceList/devicerelslave
    console.log(row);
    //我想打开浏览器的新页签显示
    //window.location.href = "/#/deviceInfoSingle/"+row.deviceTemplateInfoId;
   //const newWindow = window.open("/#/deviceInfoSingle/"+row.id+"/"+row.deviceTemplateInfoId, '_blank');
   deviceValueDialogRef1.value.openDialog(row.id+"",row.deviceTemplateInfoId+"");
  };

  // 打开设备日志页面
  const openDeviceLogDialog = (row: any) => {
    deviceValueDialogRef.value.openDialog(row.id+"");

  };

    // 打开设备日志页面
   const openDeviceCurrentDialog = (row: any) => {
      deviceCurrentDialogRef.value.openDialog(row.id+"");
  };
  // 打开打印页面
  const openPrintDeviceInfo = async (row: any) => {
    printDeviceInfoTitle.value = '打印设备信息';
  }
  
  // 打开编辑页面
  const openEditDeviceInfo = (row: any) => {
    editDeviceInfoTitle.value = '编辑设备信息';
    editDialogRef.value.openDialog(row);
  };

  // 删除
  const delDeviceInfo = (row: any) => {
    ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
  .then(async () => {
    await deleteDeviceInfo(row);
    handleQuery();
    ElMessage.success("删除成功");
  })
  .catch(() => {});
  };

  // 改变页面容量
  const handleSizeChange = (val: number) => {
    tableParams.value.pageSize = val;
    handleQuery();
  };

  // 改变页码序号
  const handleCurrentChange = (val: number) => {
    tableParams.value.page = val;
    handleQuery();
  };

  const deviceGateWayDeviceGateWayIdDropdownList = ref<any>([]); 
  const getDeviceGateWayDeviceGateWayIdDropdownList = async () => {
    let list = await getDeviceGateWayDeviceGateWayIdDropdown();
    deviceGateWayDeviceGateWayIdDropdownList.value = list.data.result ?? [];
  };
  getDeviceGateWayDeviceGateWayIdDropdownList();
  
  const deviceTemplateInfoDeviceTemplateInfoIdDropdownList = ref<any>([]); 
  const getDeviceTemplateInfoDeviceTemplateInfoIdDropdownList = async () => {
    let list = await getDeviceTemplateInfoDeviceTemplateInfoIdDropdown();
    deviceTemplateInfoDeviceTemplateInfoIdDropdownList.value = list.data.result ?? [];
  };
  getDeviceTemplateInfoDeviceTemplateInfoIdDropdownList();
  
  handleQuery();
</script>
<style scoped>
:deep(.el-input),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>

